package com.easymin.weixin.controller.v5;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.easymin.weixin.common.IpHelper;
import com.easymin.weixin.controller.v5.utils.ErrorMapV5;
import com.easymin.weixin.controller.v5.utils.IPDataHandler;
import com.easymin.weixin.controller.v5.utils.ResultV5;
import com.easymin.weixin.entity.WxAccount;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.WxAccountManager;
import com.easymin.weixin.manager.WxApplicationManager;

/**
 * 微信V5版本首页控制器
 *
 * @author zane
 */
@Controller
@RequestMapping("v5/")
public class IndexController {

    @Autowired
    private WxApplicationManager wxApplicationManager;

    @Autowired
    private WxAccountManager wxAccountManager;


    /**
     * 跳转到主页
     *
     * @param appKey 系统Key
     * @param appId  微信AppId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "{appKey}", method = {RequestMethod.GET})
    public ModelAndView index(@PathVariable("appKey") String appKey, String appId, HttpServletResponse response) throws Exception {
//        System.out.println("");
        // 验证参数
        if (null == appKey || null == appId) {
            throw new Exception("参数错误");
        }
        // 查询应用
        WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
        if (null == wxApplication) {
            throw new Exception("没有该应用信息");
        }

        // 获取该应用账号
        WxAccount wxAccount = wxAccountManager.findBy(appId, wxApplication.getId());
        Cookie cookie = new Cookie("trackId", String.valueOf(RandomUtils.nextLong(1, 1000)));
        cookie.setHttpOnly(true);
        // 1小时
        cookie.setMaxAge(3600);
        response.addCookie(cookie);
        // 返回首页数据
        ModelAndView mv = new ModelAndView();
        mv.addObject("appKey", appKey);
        mv.addObject("wxApplication", wxApplication);
        mv.addObject("wxAccount", wxAccount);
        mv.setViewName("v5/index");
        return mv;
    }

    /**
     * 获取请求主机IP和IP归属地
     *
     * @param request 请求对象
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getClientIp", method = {RequestMethod.GET})
    public ResultV5 getClientIp(HttpServletRequest request) {
        try {
            String ip = IpHelper.getIp(request);
            String address = IPDataHandler.findGeography(ip);
            ResultV5 result = ErrorMapV5.ERR_SUCCESS.clone();
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("IP", ip);
            data.put("address", address);
            result.setData(data);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return ErrorMapV5.ERR_SYSTEM;
        }
    }
}
